package com.haier.controller.protal;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haier.constant.Constant;
import com.haier.constant.URLConstant;
import com.haier.utils.EmptyUtils;
import com.haier.utils.HttpUtils;
import com.haier.vo.ResponseVo;

@RestController
@RequestMapping(value = "/hipassjava/protal")
public class ProtalContoller {
	private static final Logger logger = LoggerFactory.getLogger(ProtalContoller.class);

	/**
	 * 重定向
	 */
	@RequestMapping(value = "/redirectToAuth", method = RequestMethod.POST)
	public Object redirectToAuth(HttpServletRequest request, HttpServletResponse response) {
		try {
			String url = URLConstant.PORTAL_OAUTH + "/authorize?state=sso&client_id=" + Constant.PORTAL_CLIENT_ID
					+ "&response_type=code&redirect_uri=";
			JSONObject result = new JSONObject();
			result.put("url", url);
			return ResponseVo.success(result);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 登录
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Object login(@RequestBody JSONObject params, HttpServletRequest request, HttpServletResponse response) {
		try {
			String code = params.getString("code");
			if (EmptyUtils.isEmpty(code)) {
				return ResponseVo.checkError("code不能为空");
			}
			StringBuffer url = new StringBuffer();
			url.append(URLConstant.PORTAL_OAUTH).append(URLConstant.PORTAL_TOKEN);
			url.append("?client_id=").append(URLEncoder.encode(Constant.PORTAL_CLIENT_ID, "UTF-8"));
			url.append("&client_secret=").append(URLEncoder.encode(Constant.PORTAL_CLIENT_SECRET, "UTF-8"));
			url.append("&grant_type=").append(URLEncoder.encode("authorization_code", "UTF-8"));
			url.append("&code=").append(URLEncoder.encode(code, "UTF-8"));
			String tokenStr = HttpUtils.post(url.toString(), null);
			if (EmptyUtils.isEmpty(tokenStr)) {
				return ResponseVo.checkError("code无效");
			}
			JSONObject result = new JSONObject();
			JSONObject tokenJson = JSON.parseObject(tokenStr);
			String accessToken = tokenJson.getString("access_token");
			if (EmptyUtils.isEmpty(accessToken)) {
				return ResponseVo.checkError("code无效");
			}
			result.put("token", tokenJson);

			url = new StringBuffer();
			url.append(URLConstant.PORTAL_OAUTH).append(URLConstant.PORTAL_USERINFO);
			url.append("?client_id=").append(Constant.PORTAL_CLIENT_ID).append("&access_token=").append(accessToken);
			String userStr = HttpUtils.get(url.toString(), null);
			JSONObject userJson = JSON.parseObject(userStr);
			result.put("userInfo", userJson);

			return ResponseVo.success(result);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

	/**
	 * 根据token获取用户信息
	 */
	@RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
	public Object getUserInfo(@RequestBody JSONObject params, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			String accessToken = params.getString("accessToken");
			if (EmptyUtils.isEmpty(accessToken)) {
				return ResponseVo.checkError("token不能为空");
			}
			JSONObject result = new JSONObject();
			StringBuffer url = new StringBuffer();
			url.append(URLConstant.PORTAL_OAUTH).append(URLConstant.PORTAL_USERINFO);
			url.append("?client_id=").append(Constant.PORTAL_CLIENT_ID).append("&access_token=").append(accessToken);
			String userStr = HttpUtils.get(url.toString(), null);
			JSONObject userJson = JSON.parseObject(userStr);
			result.put("userInfo", userJson);

			return ResponseVo.success(result);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return ResponseVo.error();
		}
	}

}
